
<template>
  <div class="dashboard-container">
    <el-drawer ref="drawer" size="600px" :title="status==1?'新增':'编辑'" :visible.sync="showDialog" :wrapper-closable="true" @close="close">
      <div class="detial">
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="90px" class="demo-ruleForm">
          <el-form-item label="口岸名称:" prop="port_name">
            <el-input v-model="ruleForm.port_name" placeholder="请输入口岸名称" />
          </el-form-item>
          <el-form-item label="口岸代码:" prop="code">
            <el-input v-model="ruleForm.code" placeholder="请输入口岸代码" />
          </el-form-item>

          <el-form-item label="所在国家:" prop="country">
            <el-select
              v-model="ruleForm.country"
              style="width:100%"
              placeholder="请选择所在国家"
            >
              <el-option
                v-for="(item,index) in countryCodeList"
                :key="index"
                :label="item.country_name"
                :value="item.areas_id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="自定义字段:" prop="custom">
            <el-input v-model="ruleForm.custom" placeholder="请输入自定义字段" />
          </el-form-item>
          <el-form-item label="备注:" prop="desc">
            <el-input v-model="ruleForm.desc" type="textarea" placeholder="请输入备注" />
          </el-form-item>
          <el-form-item label="状态:" prop="status">
            <el-select
              v-model="ruleForm.status"
              style="width:100%"
              placeholder="请选择状态"
            >

              <el-option
                label="启用"
                :value="1"
              />
              <el-option
                label="禁用"
                :value="2"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div class="buttons">
        <el-button
          v-if="status == 1"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="addNews()"
        >
          新 增
        </el-button>
        <el-button
          v-if="status == 2"
          size="mini"
          type="primary"
          style="margin-left:20px"
          @click="editNews()"
        >
          保 存
        </el-button>
        <el-button
          size="mini"
          type=""
          @click="close()"
        >
          取消
        </el-button>

      </div>

    </el-drawer>
  </div>
</template>

<script>
import { createPort, updatePort } from '@/api/base/port'
export default {
  name: 'Detial',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detial: {
      require: false,
      type: Object,
      default: () => {

      }
    },
    status: {
      type: Number,
      default: 1
    },
    countryCodeList: {
      require: false,
      type: Array,
      default: () => {

      }
    }
  },
  data() {
    return {
      showDialog: false,
      codes: [],
      ruleForm: {
        'port_name': '',
        'code': '',
        'country': '',
        'custom': '',
        'desc': '',
        'status': ''
      },
      rules: {
        port_name: [
          { required: true, message: '请输入口岸名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入口岸代码' }
        ],
        country: [
          { required: true, message: '请选择所在国家' }
        ],
        status: [
          { required: true, message: '请选择状态' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.showDialog = this.visible
      if (!this.showDialog) {
        this.resetForm()
      } else {
        this.codes = []
        if (this.status === 2) {
          this.ruleForm = this.detial
          if (this.detial.country) {
            this.ruleForm.country = this.detial.country.areas_id
          }
        }
      }
    }
  },
  mounted() {
  },
  methods: {
    changeCodes() {
      this.ruleForm.end_country_codes = this.codes.toString()
    },
    close() {
      this.$emit('update', false)
      this.ruleForm = {
        'port_name': '',
        'code': '',
        'country': '',
        'custom': '',
        'desc': '',
        'status': ''
      }
    },
    resetForm() {
      this.$refs['ruleForm'].resetFields()
    },
    addNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          createPort(this.ruleForm).then(res => {
            this.$message.success('添加成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    },
    editNews() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          updatePort({
            'id': this.ruleForm.id,
            'port_name': this.ruleForm.port_name,
            'custom': this.ruleForm.custom,
            'code': this.ruleForm.code,
            'country': this.ruleForm.country,
            'desc': this.ruleForm.desc,
            'status': this.ruleForm.status
          }).then(res => {
            this.$message.success('编辑成功')
            this.$emit('reload')
          })
        } else {
          return false
        }
      })
    }

  }

}
</script>
<style scoped>
.detial{
  padding:40px 40px 20px 20px;
}
.buttons{
  display: flex;
  flex-direction: row-reverse;
  padding-right: 40px;
}
</style>
